﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    /// <summary>
    /// 请先在配置文件设置ResPath       
    /// </summary>
    public static class UrlExtensions
    {

        /// <summary>
        /// 获取脚本资源地址
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        public static HtmlString Script(this UrlHelper helper, string contentPath)
        {
            const string template = " <script src='{0}' type='text/javascript'></script>";

            return GetUrl(helper, template, contentPath);
        }

        /// <summary>
        /// 获取样式表资源地址
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        public static HtmlString Css(this UrlHelper helper, string contentPath)
        {
            const string template = "<link href='{0}' rel='stylesheet' type='text/css' />";

            return GetUrl(helper, template, contentPath);
        }

        /// <summary>
        /// 根据配置文件生成资源地址
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="template"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        private static HtmlString GetUrl(this UrlHelper helper, string template, string contentPath)
        {
            string url = GetUrlString(helper, template, contentPath);

            return new HtmlString(url);
        }

   
        /// <summary>
        /// Res地址
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="template"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        public static string GetUrlResString(this UrlHelper helper, string contentPath)
        {
            return GetUrlString(helper,"{0}",contentPath);
        }


        /// <summary>
        /// 根据配置文件生成资源地址
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="template"></param>
        /// <param name="contentPath"></param>
        /// <returns></returns>
        public static string  GetUrlString(this UrlHelper helper, string template, string contentPath)
        {
            string url = yq.ControllerBase.WebConfig.ResPath;

            //组合地址
            url = url + contentPath;

            //如果是本地站点
            if (url.StartsWith("~") || url.StartsWith(".") || url.StartsWith("/"))
            {
                url = helper.Content(url);
            }

            if (yq.ControllerBase.WebConfig.IsDevelop)
            {
                url += "?v=" + DateTime.Now.Ticks.ToString();
            }


            url = string.Format(template, url);

            return url;
        }
        

    }
}
